package com.henu.art.service.Imlp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.henu.art.entity.EnvEntity;
import com.henu.art.mapper.EnvMapper;
import com.henu.art.util.EncodeUtil;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class CustomRedis {
    @Resource
    private EnvMapper envMapper;

    @Cacheable(value = "sys_env", key = "'admin_username'")
    public String getAdminUsername() {
        return envMapper.selectOne(new QueryWrapper<EnvEntity>().eq("k", "admin_username")).getV();
    }

    @Cacheable(value = "sys_env", key = "'admin_pwd'")
    public String getAdminPassword() {
        return envMapper.selectOne(new QueryWrapper<EnvEntity>().eq("k", "admin_pwd")).getV();
    }

    @CacheEvict(value = "sys_env", key = "'admin_username'")
    public boolean setAdminUsername(String username) {
        return envMapper.update(EnvEntity.builder().k("admin_username").v(username).build(), new QueryWrapper<EnvEntity>().eq("k", "admin_username")) > 0;
    }

    @CacheEvict(value = "sys_env", key = "'admin_pwd'")
    public boolean setAdminPassword(String password) {
        String encodePassword = EncodeUtil.encodePassword(password);
        return envMapper.update(EnvEntity.builder().k("admin_pwd").v(encodePassword).build(), new QueryWrapper<EnvEntity>().eq("k", "admin_pwd")) > 0;
    }
}
